﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace WorldOfMUDra
{
    public class PlayerConfigFile
    {
        string fname;
        string[] lines;
        string location;
        string name;
        List<string> visitedareas = new List<string>();
        List<string> itemids;
        List<string> abilities;

        public PlayerConfigFile(string FileName)
        {
            fname = FileName;
            if (fname.Split('.')[1].ToLower() == "xml")
            {
                Console.WriteLine("XML file extension detected. Will attempt to parse as XML document...");
                XmlTextReader reader = new XmlTextReader(fname);
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            switch (reader.Name.ToLower())
                            {
                                case "location":
                                    this.location = reader.ReadString().Trim();
                                    break;
                                case "visitedlocations":
                                    this.visitedareas = reader.ReadString().Trim().Split(' ').ToList<string>();
                                    break;
                                case "name":
                                    this.name = reader.ReadString().Trim();
                                    break;
                                case "itemids":
                                    string ids = reader.ReadString().Trim();
                                    this.itemids = new List<string>();
                                    foreach (string id in ids.Split(' ')) this.itemids.Add(id);
                                    break;
                                case "abilities":
                                    string abs = reader.ReadString().Trim();
                                    this.abilities = new List<string>();
                                    foreach (string ability in abs.Split(' ')) this.abilities.Add(ability);
                                    break;
                            }
                            break;
                    }
                }
            }
            try
            {
                lines = File.ReadAllLines(fname);
            }
            catch (Exception)
            {
                Console.WriteLine("Issues loading player config file by name: " + fname);
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
        }

        public string LocationID
        {
            get
            {
                return location;
            }
        }

        public string[] VisitedAreas
        {
            get
            {
                return visitedareas.ToArray();
            }
        }

        public string[] ItemIDs
        {
            get
            {
                return itemids.ToArray();
            }
        }

        public string[] Abilities
        {
            get
            {
                return abilities.ToArray();
            }
        }
    }
}
